<template>
  <div class="pd-20px">
    <TableForm :config="tableConfig"   ref="v2Table"></TableForm>
    <DialogForm :fullscreen="true"
                ref="df"
                :fields="dialogFormColumns"
                :form-data="formData"
                v-model="dfTpl"
                @submit="addTemplate"></DialogForm>
  </div>
</template>

<script>
import Service from "api/Service";
import http from "network/http";
import ApiResponse from "utils/ApiResponse";
import DialogForm from "components/v2-form/dialogForm";

export default {
  name: "CodeTemplate",
  components: {DialogForm},
  props:{
    projectId: ''
  },
  data(){
    return {
      dfTpl: false ,
      dialogFormColumns:[
        {
          key: 'tplName' ,
          title: "模板名称",
          required: true
        },
        {
          key: 'folder' ,
          title: "保存目录",
          required: true
        },
        {
          key: 'classSuffix' ,
          title: "类名后缀",
          required: true
        },
        {
          key: 'tplLanguage',
          title: '模板语言',
          is: "select",
          attrs:{
            config:{
              url: `/${Service.itToolService}/toolDictItem/list`,
              method: 'post',
              params:{
                dictType: 'code_language'
              },
              map:{
                label: 'dictTitle' ,
                value: 'dictTitle'
              },
            }
          },
          required: true ,
          change:(f,val)=>{
            return{
              suffix: val.dictVal
            }
          }
        },
        {
          key: 'tpl' ,
          title: "模板内容",
          is: 'CodeEdit',
          attrs:{
            lang: 'tplLanguage',
          } ,
          required: true
        },
      ],
      formData: {},
      api:{
        save: `/${Service.itToolService}/toolTpl/save` ,
        update: `/${Service.itToolService}/toolTpl/update`
      },
      apiKey: '' ,
      tableConfig:{
        url: `/${Service.itToolService}/toolTpl/listByPage`,
        method: 'post',
        params: ()=>{
          return {
            projectId: this.projectId
          }
        },
        lazy: true,
        buttons:[
          {
            title: '新增',
            type: 'ADD',
            callback: ()=>{
              this.formData = {
                projectId: this.projectId ,
                tplName: "" ,
                tplLanguage: '' ,
                classSuffix: ''
              }
              this.apiKey = "save"
              this.dfTpl = true ;
            }
          }
        ],
        columns:[
          {
            key: 'tplName' ,
            title: "模板名称",
            required: true
          },
          {
            key: 'classSuffix' ,
            title: "类名后缀",
            required: true
          },
          {
            key: 'tplLanguage',
            title: '模板语言',
          },
          {
            key: 'createTime',
            show: false ,
            title: '创建时间'
          },
          {
            type: 'buttons',
            title: '操作',
            width: 200,
            buttons: [
              {
                title: '删除',
                type: 'delete',
                id: 'tplId',
                url: `/${Service.itToolService}/toolTpl/deleteByIds`
              },
              {
                title: '修改',
                type: 'update',
                callback: (row)=>{
                  console.log("修改",row)
                  this.apiKey = "update";
                  this.formData = Object.assign({},row)
                  this.dfTpl = true
                }
              }
            ]
          }
        ]
      }
    }
  },
  watch:{
    projectId(id){
      if(id){
        this.$nextTick(()=>{
          this.$refs.v2Table.refresh();
        })
      }
    }
  },
  mounted() {
    if(this.projectId){
      this.$refs.v2Table.refresh();
    }
  },
  computed:{
  },
  methods:{
    addTemplate(formData){
      let api = this.apiKey
      http.post(this.api[api],formData).then(res=>{
        ApiResponse.init(res).print(res.msg).ok(()=>{
          this.$refs.v2Table.refresh();
        })
      })
    }
  }
}
</script>

<style scoped>

</style>
